HAL DAC initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 DAC configuration panel.

  • Configure the SW layer HAL

  • Configure the DAC channels.

  • Configure the DAC GPIO pins.

  • Configure the DAC DMA(s) if the application requires to use the DAC in DMA mode.

  • Configure the NVIC if the application requires the DAC to be used in IT mode.

  • Generate the code

Note

The configuration below might varies from a series to another.

HAL DAC configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL DAC main features configuration

Configure the DAC main features.

DAC main feature configuration

HAL DAC channels configuration

Configure the DAC channels basic features: - Configure the alignment, Trigger, Output buffer, Output connection and the data sign format.

DAC basic channel feature configuration

HAL GPIO configuration

Configure GPIO pins.

DAC GPIO configuration

HAL DMA configuration

Configure the DMA for data reception as needed.

DAC DMA configuration

HAL NVIC configuration

Configure the NVIC in case of an interrupt.

DAC NVIC configuration

Generated DAC initialization and de-initialization sequences

  • The DAC initialization sequence is generated in the mx_dac1_hal_init function in mx_dac1.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : DAC initialization and de-initialization sequences

Topic

Code Snippet HAL1

Code Snippet HAL2

Handle declaration

DAC_HandleTypeDef hdac1;
DMA_HandleTypeDef handle_LPDMA1_Channel0;
static hal_dac_handle_t hDAC1;
static hal_dma_handle_t hLPDMA1_CH0;

Initialization and configuration sequence

static void MX_DAC1_Init(void)
{

  DAC_ChannelConfTypeDef sConfig = {0};
  DAC_AutonomousModeConfTypeDef sAutonomousMode = {0};

  hdac1.Instance = DAC1;
  if (HAL_DAC_Init(&hdac1) != HAL_OK)
  {
    Error_Handler();
  }

  sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MHZ;
  sConfig.DAC_DMADoubleDataMode = DISABLE;
  sConfig.DAC_SignedFormat = DISABLE;
  sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_ENABLE;
  sConfig.DAC_Trigger = DAC_TRIGGER_SOFTWARE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
  sConfig.DAC_UserTrimming = DAC_TRIMMING_USER;
  sConfig.DAC_TrimmingValue = 31;
  sConfig.DAC_SampleAndHoldConfig.DAC_SampleTime = 62;
  sConfig.DAC_SampleAndHoldConfig.DAC_HoldTime = 12;
  sConfig.DAC_SampleAndHoldConfig.DAC_RefreshTime = 4;
  if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }

  sAutonomousMode.AutonomousModeState = DAC_AUTONOMOUS_MODE_DISABLE;
  if (HAL_DACEx_SetConfigAutonomousMode(&hdac1, &sAutonomousMode) != HAL_OK)
  {
    Error_Handler();
  }

}
static void MX_LPDMA1_Init(void)
{
  __HAL_RCC_LPDMA1_CLK_ENABLE();

    HAL_NVIC_SetPriority(LPDMA1_Channel0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(LPDMA1_Channel0_IRQn);

}
static void MX_GPIO_Init(void)
{

  __HAL_RCC_GPIOA_CLK_ENABLE();

}
void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hdac->Instance==DAC1)
  {


    __HAL_RCC_DAC1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    handle_LPDMA1_Channel0.Instance = LPDMA1_Channel0;
    handle_LPDMA1_Channel0.Init.Request = LPDMA1_REQUEST_DAC1_CH1;
    handle_LPDMA1_Channel0.Init.BlkHWRequest = DMA_BREQ_SINGLE_BURST;
    handle_LPDMA1_Channel0.Init.Direction = DMA_PERIPH_TO_MEMORY;
    handle_LPDMA1_Channel0.Init.SrcInc = DMA_SINC_INCREMENTED;
    handle_LPDMA1_Channel0.Init.DestInc = DMA_DINC_INCREMENTED;
    handle_LPDMA1_Channel0.Init.SrcDataWidth = DMA_SRC_DATAWIDTH_WORD;
    handle_LPDMA1_Channel0.Init.DestDataWidth = DMA_DEST_DATAWIDTH_WORD;
    handle_LPDMA1_Channel0.Init.Priority = DMA_LOW_PRIORITY_LOW_WEIGHT;
    handle_LPDMA1_Channel0.Init.TransferEventMode = DMA_TCEM_BLOCK_TRANSFER;
    handle_LPDMA1_Channel0.Init.Mode = DMA_NORMAL;
    if (HAL_DMA_Init(&handle_LPDMA1_Channel0) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(hdac, DMA_Handle1, handle_LPDMA1_Channel0);

    if (HAL_DMA_ConfigChannelAttributes(&handle_LPDMA1_Channel0, DMA_CHANNEL_NPRIV) != HAL_OK)
    {
      Error_Handler();
    }

    HAL_NVIC_SetPriority(DAC1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DAC1_IRQn);

  }

}
hal_dac_handle_t *mx_dac1_hal_init(void)
{
    HAL_RCC_DAC1_EnableClock();

    if (HAL_DAC_Init(&hDAC1, HAL_DAC1) != HAL_OK)
    {
        return NULL;
    }

    hal_dac_config_t dac_config;
    dac_config.high_frequency_mode = HAL_DAC_HIGH_FREQ_MODE_ABOVE_80MHZ;
    if (HAL_DAC_SetConfig(&hDAC1, &dac_config) != HAL_OK)
    {
        return NULL;
    }

    hal_dac_channel_config_t dac_channel_config;

    dac_channel_config.alignment         = HAL_DAC_DATA_ALIGN_12_BITS_RIGHT;
    dac_channel_config.trigger           = HAL_DAC_TRIGGER_SOFTWARE;
    dac_channel_config.output_buffer     = HAL_DAC_OUTPUT_BUFFER_ENABLED;
    dac_channel_config.output_connection = HAL_DAC_OUTPUT_CONNECTION_EXTERNAL;
    dac_channel_config.data_sign_format  = HAL_DAC_SIGN_FORMAT_UNSIGNED;
    if (HAL_DAC_SetConfigChannel(&hDAC1, HAL_DAC_CHANNEL_1, &dac_channel_config) != HAL_OK)
    {
        return NULL;
    }
    HAL_DAC_EnableChannelSampleAndHold(&hDAC1, HAL_DAC_CHANNEL_1);
    AL_DAC_SetChannelBufferCalibrationValue(&hdac, HAL_DAC_CHANNEL_1, 31);
    hal_dac_channel_sample_and_hold_config_t dac_channel1_sample_and_hold_config;
    dac_channel1_sample_and_hold_config.hold_time_cycle    = 12;
    dac_channel1_sample_and_hold_config.sample_time_cycle  = 62;
    dac_channel1_sample_and_hold_config.refresh_time_cycle = 4;
    if (HAL_DAC_SetConfigChannelSampleAndHold(&hDAC1,
                                              HAL_DAC_CHANNEL_1,
                                              &dac_channel1_sample_and_hold_config) != HAL_OK)
    {
        return NULL;
    }
    hal_status_t HAL_DAC_AM_Disable(&hDAC1);

    HAL_RCC_GPIOA_EnableClock();

    hal_gpio_config_t gpio_config;

    gpio_config.mode        = HAL_GPIO_MODE_ANALOG;
    gpio_config.pull        = HAL_GPIO_PULL_NO;
    HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_4, &gpio_config);

    HAL_RCC_LPDMA1_EnableClock();

    if (HAL_DMA_Init(&hLPDMA1_CH0, HAL_LPDMA1_CH0) != HAL_OK)
    {
        return NULL;
    }

    hal_dma_direct_xfer_config_t xfer_cfg_dac1_ch1_dma;
    xfer_cfg_dac1_ch1_dma.request         = HAL_LPDMA1_REQUEST_DAC1_CH1;
    xfer_cfg_dac1_ch1_dma.direction       = HAL_DMA_DIRECTION_PERIPH_TO_MEMORY;
    xfer_cfg_dac1_ch1_dma.src_inc         = HAL_DMA_SRC_ADDR_INCREMENTED;
    xfer_cfg_dac1_ch1_dma.dest_inc        = HAL_DMA_DEST_ADDR_INCREMENTED;
    xfer_cfg_dac1_ch1_dma.src_data_width  = HAL_DMA_SRC_DATA_WIDTH_WORD;
    xfer_cfg_dac1_ch1_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_WORD;
    xfer_cfg_dac1_ch1_dma.priority        = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;

    if (HAL_DMA_SetConfigPeriphDirectXfer(&hLPDMA1_CH0, &xfer_cfg_dac1_ch1_dma) != HAL_OK)
    {
        return NULL;
    }

    HAL_CORTEX_NVIC_SetPriority(LPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
    HAL_CORTEX_NVIC_EnableIRQ(LPDMA1_CH0_IRQn);

    HAL_DAC_SetChannelDMA(&hDAC1, &hLPDMA1_CH0, HAL_DAC_CHANNEL_1);

    return &hDAC1;
    HAL_CORTEX_NVIC_SetPriority(DAC1_IRQn,
                   HAL_CORTEX_NVIC_PREEMP_PRIORITY_0,
                   HAL_CORTEX_NVIC_SUB_PRIORITY_0);
    HAL_CORTEX_NVIC_EnableIRQ(DAC1_IRQn);
}

Deinitialization sequence

void MX_DAC_DeInit(void)
{
  if (HAL_DAC_DeInit(&hdac) != HAL_OK)
  {
    Error_Handler();
  }
}
void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
{
  if(hdac->Instance==DAC1)
  {
    __HAL_RCC_DAC1_CLK_DISABLE();

    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4);

    HAL_DMA_DeInit(hdac->DMA_Handle1);

    HAL_NVIC_DisableIRQ(DAC1_IRQn);

  }

}
void mx_dac1_hal_deinit(void)
{
    (void)HAL_DAC_DeInit(&hDAC1);
    HAL_RCC_DAC1_Reset();
    HAL_RCC_DAC1_DisableClock();

    HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_4);

    HAL_DMA_DeInit(&hLPDMA1_CH0);

    HAL_CORTEX_NVIC_DisableIRQ(LPDMA1_CH0_IRQn);
}

IRQ handle function

void DAC1_IRQHandler(void)
{
  HAL_DAC_IRQHandler(&hdac1);
}

void LPDMA1_Channel0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&handle_LPDMA1_Channel0);
}
void DAC1_IRQHandler(void)
{
    HAL_DAC_IRQHandler(&hDAC1);
}

void LPDMA1_CH0_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}